package com.jukte.recipe;

import java.util.ArrayList;
import java.util.List;

import com.ewhat.recipes.R;
import com.jukte.recipe.adapters.AddtoCartAdapter;
import com.jukte.recipe.db.DataBaseManager;
import com.jukte.recipe.models.Ingredient;
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.ProgressDialog;
import android.content.Context;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

public class AddToCartActivity extends BaseActivity implements OnClickListener{

	private Context context;
	private ListView list;
	private Button selectAllBtn,deselectAllBtn;
	private List<Ingredient> ingredients;
	private int recipeId;
	private AddtoCartAdapter adapter;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_add_to_cart);
		
		init();
		
		getIntentData();
		setUpList();
		new GetIngredientsDataTask().execute();
	}
	
	@Override
	public void init() {
		super.init();
		imb_search.setVisibility(View.GONE);
		imb_shoppingcart.setVisibility(View.GONE);
		imb_save.setVisibility(View.VISIBLE);
		tv_title.setText("Add to Shopping List");

		context=AddToCartActivity.this;
		list=(ListView)findViewById(R.id.list);
		selectAllBtn=(Button)findViewById(R.id.select_all_btn);
		selectAllBtn.setOnClickListener(this);
		deselectAllBtn=(Button)findViewById(R.id.deselect_all_btn);
		deselectAllBtn.setOnClickListener(this);
		imb_save.setOnClickListener(this);
		ingredients=new ArrayList<Ingredient>();
	}

	private void setUpList() {
		// TODO Auto-generated method stub
		adapter=new AddtoCartAdapter(AddToCartActivity.this, ingredients);
		list.setAdapter(adapter);
	}

	private void fetchData() {
		ingredients.clear();
		ingredients.addAll(DataBaseManager.getInstance().getAllIngredients(recipeId));
		DataBaseManager.getInstance().closeDb();
	}
	
	
	public class GetIngredientsDataTask extends AsyncTask<Void, Void, Void> 
    {
        public ProgressDialog progDialog=null;
        
        
        
        protected void onPreExecute() 
        {
            progDialog=ProgressDialog.show(context, "", "Loading...",true);
        }
        @Override
        protected Void doInBackground(Void... params) 
        {
           fetchData();
           return null;
        }    
        protected void onPostExecute(Void result) 
        {
            if(progDialog.isShowing())
            {
                progDialog.dismiss();
            }
            updateUi();
        }
		
    }
	private void updateUi() {
		// TODO Auto-generated method stub
		adapter.notifyDataSetChanged();
	}

	private void getIntentData() {
		// TODO Auto-generated method stub
		recipeId=getIntent().getIntExtra("RECIPE_ID", 0);
	}

	

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		if(v==selectAllBtn)
		{
			for(int i=0;i<ingredients.size();i++)
			{
				ingredients.get(i).setIsAddedToCart(1);
			}
			adapter.notifyDataSetChanged();
		}
		else if(v==deselectAllBtn)
		{
			for(int i=0;i<ingredients.size();i++)
			{
				ingredients.get(i).setIsAddedToCart(0);
			}
			adapter.notifyDataSetChanged();
		}
		else if(v==imb_save)
		{
			ingredients=adapter.getItems();
			new UpdateCartDataTask().execute();
		}
	}
	 
	public class UpdateCartDataTask extends AsyncTask<Void, Void, Void> 
    {
        public ProgressDialog progDialog=null;

        protected void onPreExecute() 
        {
            progDialog=ProgressDialog.show(context, "", "Loading...",true);
        }
        
        @Override
        protected Void doInBackground(Void... params) 
        {
        	//int flag=1;  
    		for(int i=0;i<ingredients.size();i++)
    		{
    			if(ingredients.get(i).getIsAddedToCart()==1)
    			{
    				//flag=1;
    				break;
    			}
    		}
    		DataBaseManager.getInstance().updateCart(recipeId, 1, ingredients);//always flags=1 when add to cart
    		DataBaseManager.getInstance().closeDb();
    		
           return null;
        }    
       
		protected void onPostExecute(Void result) 
        {
            if(progDialog.isShowing())
            {
                progDialog.dismiss();
            }
            //updateUi();
            Toast.makeText(context, "Successfully added to your shopping list!", Toast.LENGTH_LONG).show();
            finish();
        }
		
    }

}
